<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\Novel;
use DB;
class NovelController extends Controller
{
    //
    public function getNoveladd(){
    	return view('admin/noveladd');
  	}

  	public function postNoveladd(Request $req){       //小说添加
  		$novel = new Novel();

      //dd($req);

      // print_r($_POST);exit;

  		$user = $req->user();        //新建一个user对象，获取登录用户的信息

  		$novel->uid = $user->uid;
  		$novel->name = $user->name;
  		$novel->title = $req->title;
  		$novel->gender = $req->gender;
  		$novel->type = $req->type;
  		$novel->desc = $req->desc;
      $novel->collection = 0;
  		$novel->status = 1;
  		$novel->pubtime = time();
      //上传封面
      $path = "upload/";                                       //指定路径

      $file = $req['cover_img'];                               //取到图片文件
      $file_name = \Auth::user()->uid . time() . $file->getClientOriginalName(); //文件名
      $file->move($path, $file_name);                          //上传

      $novel->cover_img = '/' . $path . $file_name;
      // dd($novel);

  		$novel->save();             //小说信息入库

      $user->novelnum++;          //该用户的作品数量加一
      $user->save();              //用户信息入库

  		return redirect("work");
  	}

    public function girl(){
      $novel = Novel::orderBy('pubtime','desc')->take(10)->get();
      //dd($novel);
      return view('public/girl',['novel'=>$novel]);
    }

    public function boy(){
      return view('public/boy');
    }

    public function getNovelset($nid){
      $novel = Novel::find($nid);
      //dd($novel);
    	return view('admin/novelset' , ['novel' => $novel , 'nid' => $nid]);
    }

    public function postNovelset(Request $req , $nid){
      //dd($req);
      $user = Auth::user();
      $novel = Novel::find($nid);
      $uid = $user->uid;
      $name = $user->name;

      $novel->uid = $uid;
      $novel->name = $name;
      $novel->title = $req->name;
      $novel->gender = $req->gender;
      $novel->status = $req->status;
      $novel->desc = $req->content;

      $novel->type = $req->type;
      $novel->pubtime = time();
       //上传封面
      $path = "upload/";           //指定路径
                                        
      // dd($req['cover_img']);
      $file = $req['cover_img'];                               //取到图片文件

      $file_name = \Auth::user()->uid . time() . $file->getClientOriginalName(); //文件名
      $file->move($path, $file_name);                          //上传

      $novel->cover_img = '/' . $path . $file_name;
      $novel->save();
        return '修改成功';
      
    }

    public function getDraft($nid){
      $novel = Novel::find($nid);
      return view('admin/draft',['novel' => $novel , 'nid' => $nid]);
    }

    public function postDraft(Request $req , $nid){

    }
    public function ranKing(){
      return view('public/ranking');
    }
    public function getSearch(Request $req, $gender = 'all'){
      $keyword = $req->keyWord;  //获取关键字
      if($keyword){              //如果有关键字
        $novel = DB::table('novels')->where('title','like',"%$keyword%")->get();
        return view('public/search',['novel'=>$novel,'gender'=>$gender]);
      } else {
        if ($gender == 'all') {
        $novel = Novel::orderBy('pubtime','desc')->get();
        }else {
          $novel = Novel::where('gender',$gender)->orderBy('pubtime','desc')->get();
        }
        return view('public/search',['novel' => $novel,'gender' => $gender]);
      }
    }



}
